var crypto = require('crypto')
var fs = require('fs')

/**
* nodejs脚本
* 作用: 秒传
* 参数: inputFile 秒传对象路径和文件名, 因为是当前文件夹下两个参数合为一个参数了
* 参数: sid qq邮箱登录后url上面的session id 
*/
var inputFile = 'test.mp4'
var sid = 'Lb2Lgkhh2HiijEAd'

var size = fs.statSync(inputFile).size
var buffer = fs.readFileSync(inputFile)
var sha1 = calcFileSha1(buffer)
// 发现md5非必需
// var md5 = calcFileMD5(buffer)

var quickUrl = createQuickUploadUrl('', sha1, size, inputFile, sid)
console.log(quickUrl)

function calcFileSha1(buffer) {
  var fsHash = crypto.createHash('sha1')
  fsHash.update(buffer)
  var sha1 = fsHash.digest('hex').toLowerCase()
  return sha1
}

function calcFileMD5(buffer) {
  var fsHash = crypto.createHash('md5')
  fsHash.update(buffer)
  var sha1 = fsHash.digest('hex').toLowerCase()
  return sha1
}

function createQuickUploadUrl(md5, sha1, size, name, sid) {
  return `https://mail.qq.com/cgi-bin/ftnCreatefile?uin=&ef=js&resp_charset=UTF8&s=ftnCreate&sid=${sid}&dirid=&path=${name}&size=${size}&md5=${md5}&sha=${sha1}&sha3=&appid=2&loc=ftnCreatefile,ftnCreatefile,ftnCreate,ftn2`
}